﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientAPI;

namespace Client_test
{
    public partial class ChatWindow : Form
    {
        string _curentTab = "ALL";

        public ChatWindow()
        {
            InitializeComponent();
            fakeTabsBOX.Text = _curentTab;
            ClientAPI.Model.Instance._updateChatHandler += UpdateChatPage;
            ClientAPI.Model.Instance._currentChatsHandler += UpdateCurrentChats;
            ClientAPI.Model.Instance._chatListHandler += UpdateChatList;
        }

        private void sendBTN_Click(object sender, EventArgs e)
        {
            if (ClientAPI.Model.Instance.IsConnected())
            {
                if (sendTXT.Text != "" || sendTXT.Text != null)
                {
                    ClientAPI.Model.Instance.SendChatMessage(_curentTab, sendTXT.Text);
                }

                sendTXT.Text = "";
            }
            else
                MessageBox.Show("warning not connected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void UpdateChatPage(object sender, UpdateChatEventArgs eargs)
        {
            if (chatPage.InvokeRequired)
            {
                chatPage.Invoke((MethodInvoker)delegate
                {
                    chatPage.Text = ClientAPI.Model.Instance.GetChatLog(_curentTab);
                });
            }
            else
                chatPage.Text = ClientAPI.Model.Instance.GetChatLog(_curentTab);
        }

        private void UpdateCurrentChats(object sender, UpdateCurrentChatsEventArgs eargs)
        {
            if (fakeTabsBOX.InvokeRequired)
                fakeTabsBOX.Invoke((MethodInvoker)delegate
                {
                    fakeTabsBOX.Items.Clear();
                    fakeTabsBOX.Items.AddRange(ClientAPI.Model.Instance.GetCurrentChats());
                });
            else
            {
                fakeTabsBOX.Items.Clear();
                fakeTabsBOX.Items.AddRange(ClientAPI.Model.Instance.GetCurrentChats());
            }
        }

        private void UpdateChatList(object sender, UpdateChatListEventArgs eargs)
        {
            if (chatListBOX.InvokeRequired)
                chatListBOX.Invoke((MethodInvoker)delegate
                {
                    chatListBOX.Items.Clear();
                    chatListBOX.Items.AddRange(ClientAPI.Model.Instance.GetChatList());
                });
            else
                chatListBOX.Items.AddRange(ClientAPI.Model.Instance.GetChatList());
        }

        private void fakeTabsBOX_SelectedIndexChanged(object sender, EventArgs e)
        {
            _curentTab = fakeTabsBOX.Text;
            ClientAPI.Model.Instance.SetCurrentTab(_curentTab);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _curentTab = chatListBOX.SelectedItem.ToString();
            fakeTabsBOX.Text = _curentTab;
        }

        private void closeChatBTN_Click(object sender, EventArgs e)
        {
            if (fakeTabsBOX.SelectedIndex != 0)
            {
                fakeTabsBOX.Items.RemoveAt(fakeTabsBOX.SelectedIndex);
                _curentTab = fakeTabsBOX.Text;

                ClientAPI.Model.Instance.SetCurrentTab(_curentTab);
            }
        }

        private void sendTXT_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (ClientAPI.Model.Instance.IsConnected())
                {
                    if (sendTXT.Text != "" || sendTXT.Text != null)
                    {
                        ClientAPI.Model.Instance.SendChatMessage(_curentTab, sendTXT.Text);
                    }

                    sendTXT.Text = "";
                }
                else
                    MessageBox.Show("warning not connected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
